package compare;

import java.util.Comparator;

/**
 * @author yuisama
 * @date 2022/01/07 19:54
 **/
class AgeSec implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        if (o1.getAge() == o2.getAge()) {
            return 0;
        } else if (o1.getAge() > o2.getAge()) {
            return 1;
        }
        return -1;
    }
}

class AgeDeSec implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        if (o1.getAge() == o2.getAge()) {
            return 0;
        } else if (o1.getAge() > o2.getAge()) {
            return -1;
        }
        return 1;
    }
}




public class Student {
    private String name;
    private int age;

    public int getAge() {
        return age;
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

//    @Override
//    public int compareTo(Student o) {
//        if (this.age == o.age) {
//            return 0;
//        }else if (this.age < o.age) {
//            return  - 1;
//        }
//        return 1;
//    }

    // 如果我要比较两个Student对象是否相等，就需要覆写Object类提供的equals
//    @Override
//    public boolean equals(Object o) {
//        if (this  == o) {
//            return true;
//        }
//        if (o == null || !(o instanceof Student)) {
//            // 传入对象为空或不是Student类型对象
//            return false;
//        }
//        // 此时O一定不是空且是Student对象，脱掉o的小马甲
//        Student stu = (Student) o;
//        return this.name.equals(stu.name);
//    }
}